﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Web;
namespace Li.DingTalk.Helper
{
    public static class StrHelper
    {
        public static string AddPoint(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            str = string.Format(",{0},", str);
            return str;
        }

        public static string RemovePoint(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            str = str.Trim(',');
            return str;
        }


        #region 过滤特殊字符
        /// <summary>
        /// 过滤特殊字符
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string StringToJson(String s)
        {
            System.Text.StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];

                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
        #endregion

        public static string RemoveSpecialStr(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            try
            {
                String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？ ]";
                Regex rgx = new Regex(regEx);
                return rgx.Replace(str, "");
            }
            catch (Exception ex)
            {
                //ex不处理
                return str;
            }
        }

        //public static string InsertRequestParam(this string source, string requestKey, string value = null)
        //{
        //    if (value != null)
        //    {
        //        source += "&" + requestKey + "=" + value;
        //    }
        //    else if (!string.IsNullOrEmpty(HttpContext.Current.Request[requestKey]))
        //    {
        //        source += "&" + requestKey + "=" + HttpContext.Current.Request[requestKey];
        //    }
        //    return source;
        //}
    }
}
